// pages/register/register.js

const db = wx.cloud.database().collection('userList')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name:"",
        no:"",
        tel:'',
        userList:[],
        isUser:false
    },

        //获取输入姓名
        getNameInput(e){
            this.setData({
                name : e.detail
            })
            var name = this.data.name
            console.log("name is : " + name) 
        },
        //获取输入工号
        getNoInput(e){
            this.setData({
                no:e.detail
            })
            var no = this.data.no
            console.log("no is : " + no);
        },
        //获取电话
        getTelInput(e){
            this.setData({
                tel:e.detail
            })
            var tel = this.data.tel
            console.log("tel is : " + tel);
        },

        //用户注册
        userRegister(){
            //判断用户是否已存在
            //回调时重置
            this.setData({
                isUser:false
            })
            var is = this.data.isUser
            console.log("回调重置：");
            console.log(is);
            var userList = this.data.userList
            var name = this.data.name
            var no = this.data.no
            var tel = this.data.tel

            if (name == "" || no == "" || tel == "") {
                wx.showToast({
                  title: '输入不能为空!',
                })
            }
            else{
                    for (let i = 0; i < userList.length; i++) {
                    if (name == userList[i].name && no == userList[i].no || no == userList[i].no) {
                        this.setData({
                            isUser:true
                        })
                        var is = this.data.isUser
                        console.log("当前注册用户是否存在：");
                        console.log(is);
                    }
                }
                var is = this.data.isUser
                if (is) {
                    wx.showToast({
                        title: '当前用户已存在,请返回登录',
                        icon:'none'
                    })
                }
                else{
                    db.add({
                        data:{
                            name:name,
                            no:no,
                            tel:tel
                        },
                        success:(res)=>{
                            console.log(res);
                            wx.showToast({
                            title: '注册成功',
                            icon:'success'
                            })
                            wx.navigateTo({
                              url: '/pages/login/login',
                            })
                        },
                        fail:(res)=>{
                            wx.showToast({
                              title: '注册失败',
                              icon:'error'
                            })
                            
                        }
                    })
                }
            }
            
            },


        onLoad(){
            db.get({
                success:(res)=>{
                    console.log("用户列表：");
                    console.log(res.data);
                    this.setData({
                        userList:res.data
                    })
                }
            })
        }
})